package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.Point;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

/**
 * 限时兑换
 *
 * @author Dell
 */
public class ActiveModule_限时兑换 extends PlayerModule {

    //bean ------------------------------------------------------

    public static final int acttype = 8;

    private DateTime start;
    private DateTime obtainend;        //获取奖励结束时间
    private DateTime exchangeend;    //兑换结束时间

    public DateTime getStart() {
        return start;
    }

    public void setStart(DateTime start) {
        this.start = start;
    }

    public DateTime getObtainend() {
        return obtainend;
    }

    public void setObtainend(DateTime obtainend) {
        this.obtainend = obtainend;
    }

    public DateTime getExchangeend() {
        return exchangeend;
    }

    public void setExchangeend(DateTime exchangeend) {
        this.exchangeend = exchangeend;
    }

    // func -----------------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

    /**
     * @param pcode
     */
    public static ActiveModule_限时兑换 create(String pcode) {
        PlayerBean pb = PlayerBean.get(pcode);
        int sid = pb.getServerId();

        ServerBean sb = ServerBean.get(sid);
        int areaid = sb.getAreaId();

        activeControl sai = activeControl.get(areaid, acttype);
        if (sai == null) {
            return null;
        }

        ActiveModule_限时兑换 am = new ActiveModule_限时兑换();
        am.setPlayerCode(pcode);
        am.setTimeline(sai.getTimeline());

        DateTime start = sai.getTimeline().getStartTime();
        DateTime end = sai.getTimeline().getEndTime();

        DateTime duihuanexchangeend = end;
        DateTime benlunhuodongend = duihuanexchangeend.plusDays(-2);

        if (benlunhuodongend.isBefore(start)) {
            return null;
        }

        am.setStart(start);
        am.setObtainend(benlunhuodongend);
        am.setExchangeend(duihuanexchangeend);

        am.set();
        return am;

    }

    public void update() {
        PlayerBean pb = PlayerBean.get(getPlayerCode());
        ServerBean sb = ServerBean.get(pb.getServerId());
        activeControl sai = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(acttype));
        if (sai == null) {
            return;
        }

        // 关闭活动
        if (!sai.running() && running()) {
            setTimeline(sai.getTimeline());
            set();
        }

        // 开启活动
        if (sai.running() && !running()) {
            //TODO: 充值活动时间，货币

            DateTime start = sai.getTimeline().getStartTime();
            DateTime end = sai.getTimeline().getEndTime();

            DateTime duihuanexchangeend = end;
            DateTime benlunhuodongend = duihuanexchangeend.plusDays(-2);

            if (benlunhuodongend.isBefore(start)) {
                return;
            }

            //清除限时兑换积分
            long point = Point.get(getPlayerCode(), CurrencyType.CT_限时兑换积分, false);
            if (point != 0) {
                try {
                    Point.minus(getPlayerCode(), CurrencyType.CT_限时兑换积分, point);
                } catch (CurrencyNotEnoughException e) {
                    e.printStackTrace();
                }
            }

            setTimeline(sai.getTimeline());
            setStart(start);
            setObtainend(benlunhuodongend);
            setExchangeend(duihuanexchangeend);

            set();
        }

    }

    // redis ---------------------------------------------------------

    static private String key(String playerCode) {
        return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
    }

    public void set() {
        try {
            String key1 = key(getPlayerCode());
            setv(key1, this);
            check();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 ActiveModule_限时兑换
     *
     * @param playerCode
     * @return
     */
    static public ActiveModule_限时兑换 get(String playerCode, Boolean isNewPlayer) {
        try {
            ActiveModule_限时兑换 module = getv(key(playerCode));

            //新用户不需要去查数据库
            if (module == null && !isNewPlayer) {
                module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_限时兑换.class);
                if (module != null) {
                    module.set();
                }//同步到redis
            }

            if (module == null) {
                module = create(playerCode);
            }
            if (module == null) {
                return null;
            }

            module.update();
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查看是否有可领取奖励 控制红点
     */
    public void check() {
        if (running()) {
            if (DateTime.now().isBefore(getObtainend())) {
                new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
                return;
            }

            if (DateTime.now().isAfter(getObtainend()) && DateTime.now().isBefore(getExchangeend())) {
                new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.活动兑换未关闭);
                return;
            }
        }

        new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
        return;
    }

    // lock==========================================================================
    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ActiveModule_限时兑换 activeModule_限时兑换 = getv(key(playerCode));
                MySQLCacheDAO.setV(key, activeModule_限时兑换);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

}
